<?php
namespace app\admini\controller;
use think\Db;
use think\Controller;
use think\captcha\Captcha;
use think\Session;

class Login extends Controller
{
	public function login()
	{
		if(session('ADMIN')){
			$this->redirect('index/index');
		}
		if($this->request->isPost()){
			$data = $this->request->post();
			if(!captcha_check($data['verity'] )){
				return json(['code'=>2,'message'=>'验证不对，请重新输入']);
			}
			$res = db('auth_admini')->where('login_name',$data['username'])->find();
			if($res['login_pwd'] == md5($data['password'].'czit')){
				if ($res['status'] != 1) {
						return json(['code'=>4,'message'=>'当前用户已禁用']);
					} else {
						session('ADMIN', $res);
						db('auth_admini')->update(array(
							'last_login_time' => now_time(),
							'last_login_ip' => $this->request->ip(),
							'id' => $res['id'],
							'session_id' => session_id() //更改登录状态
						));
						return json(['code'=>1,'url'=>'/admini/index/index','message'=>'登陆成功']);
					}
				
			}else{
				return json(['code'=>3,'message'=>'登陆失败']);
			}
		}else{
			return $this->fetch();	
		}
	}
	public function tuichu()
	{
		Session::clear();
		return json(['code'=>1,'url'=>'/admini/login/login','message'=>'退出成功!']);
	}
}
